﻿

/*
* Project: HOERP
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2011年5月14日 星期六 凌晨 0:45:04
*/



package com.serp.dao.xt;

import org.tulip.ProjectLog;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.tulip.DaoException;
import org.tulip.RootDao;
import org.tulip.util.BeanUtil;

/**
 * 客户类别Dao
 */
public class CustomTypeDao extends RootDao {
	/**
	 * Logger for this class
	 */
	private static final ProjectLog logger = ProjectLog.getLogger(CustomTypeDao.class);

	@Override
	protected Object convert(ResultSet rs, List list) throws SQLException {
		CustomType current = new CustomType();
		if (list.contains("CUST_TYPE_ID"))
			current.setCustTypeId(rs.getLong("CUST_TYPE_ID"));
		if (list.contains("CUST_TYPE_NAME"))
			current.setCustTypeName(rs.getString ("CUST_TYPE_NAME"));
		if (list.contains("CUST_MATE_DISC"))
			current.setCustMateDisc(rs.getBigDecimal("CUST_MATE_DISC"));
		if (list.contains("PROC_DISC"))
			current.setProcDisc(rs.getBigDecimal("PROC_DISC"));
		if (list.contains("CUST_TYPE_REMA"))
			current.setCustTypeRema(rs.getString ("CUST_TYPE_REMA"));
		if (list.contains("CUST_TYPE"))
			current.setCustType(rs.getInt("CUST_TYPE"));
		return current;
	}
	
	/**
	 * 从客户类别ID获取客户类别
	 */
	public CustomType getCustomTypeByID(long custTypeId) throws DaoException{
		logger.debug("CustomType getCustomTypeByID("+custTypeId+") - run"); 
		return (CustomType)queryObject("SELECT CUST_TYPE_ID,CUST_TYPE_NAME,CUST_MATE_DISC,PROC_DISC,CUST_TYPE_REMA,CUST_TYPE  FROM XT_CUSTOM_TYPE_T WHERE CUST_TYPE_ID=" + custTypeId);
	}
	
	/**
	 * 添加客户类别
	 */
	public int addCustomType(CustomType obj) throws DaoException {
		logger.debug("addCustomType(CustomType) - start"); 

		int result = 0;
		List paramList = new ArrayList();
		
		long custTypeId= querySequence("SEQ_XT_CUSTOM_TYPE_T");
		paramList.add(custTypeId);		
		paramList.add(obj.getCustTypeName().trim());
		paramList.add(obj.getCustMateDisc());
		paramList.add(obj.getProcDisc());
		paramList.add(obj.getCustTypeRema().trim());
		paramList.add(obj.getCustType());
		
		String sql = "INSERT INTO XT_CUSTOM_TYPE_T (CUST_TYPE_ID,CUST_TYPE_NAME,CUST_MATE_DISC,PROC_DISC,CUST_TYPE_REMA,CUST_TYPE) VALUES(?,?,?,?,?,?) ";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("添加客户类别失败!\n 客户类别信息" + BeanUtil.getBeanLog(obj));
		}

		logger.debug("addCustomType(CustomType) - end"); 
		return result;
	}
	
	/**
	 * 删除客户类别
	 */
	public int delCustomType(CustomType obj) throws DaoException {
        logger.debug("delCustomType(CustomType) - start"); 
		
		int result = 0;
		List paramList = new ArrayList();
		
		paramList.add(obj.getCustTypeId());
	 
		String sql = "DELETE FROM XT_CUSTOM_TYPE_T WHERE CUST_TYPE_ID=?";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("删除客户类别失败!\n 客户类别信息" + BeanUtil.getBeanLog(obj));
		}
        
		logger.debug("delCustomType(CustomType) - end"); 
		return result;
	}
	
	/**
	 * 返回客户类别的总数
	 */
	public int getTotalCustomTypes() throws DaoException {
		logger.debug("getTotalCustomTypes()  - run"); 
		
		String sql = "SELECT COUNT(1) FROM XT_CUSTOM_TYPE_T";
		return queryInt(sql);
	}


	/**
	 * 查询客户类别并排序
	 */
	public List getCustomTypes(String property, String sortOrder,int limit, int offset) throws DaoException {
		logger.debug("List getCustomTypes  ("+property+","+ sortOrder+","+ limit+","+  offset+")- run"); 
		
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT CUST_TYPE_ID,CUST_TYPE_NAME,CUST_MATE_DISC,PROC_DISC,CUST_TYPE_REMA,CUST_TYPE  FROM XT_CUSTOM_TYPE_T");
		if (property != null && sortOrder != null) {
			sb.append(" ORDER BY ");
			sb.append(" ");
			sb.append(property);
			sb.append(" ");
			sb.append(sortOrder);
		} else{
			sb.append(" ORDER BY CUST_TYPE_ID DESC ");
		}
		
		logger.debug("List getCustomTypes(String property, String sortOrder,int limit, int offset)- end"); 
		return queryList(sb.toString(),limit,offset);
	}
	
	/**
	 * 更新客户类别
	 */
	public int updateCustomType(CustomType obj) throws DaoException {
		logger.debug("updateCustomType(CustomType) - start"); 
		
		int result = 0;
		List paramList = new ArrayList();
		
		paramList.add(obj.getCustTypeName().trim());
		paramList.add(obj.getCustMateDisc());
		paramList.add(obj.getProcDisc());
		paramList.add(obj.getCustTypeRema().trim());
		paramList.add(obj.getCustType());
		
		paramList.add(obj.getCustTypeId());
		
		String sql = "UPDATE XT_CUSTOM_TYPE_T SET CUST_TYPE_NAME=?,CUST_MATE_DISC=?,PROC_DISC=?,CUST_TYPE_REMA=?,CUST_TYPE=?  WHERE CUST_TYPE_ID=?";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("更新客户类别失败!\n 客户类别信息" + BeanUtil.getBeanLog(obj));
		}
		
		logger.debug("updateCustomType(CustomType) - end"); 
		return result;
	}
}
